package cn.hh.file;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;

import java.io.File;
import java.util.*;

/**
 * @Author huangh
 * @date 2021/11/30
 * @Description:
 */
public class DbToFileUtil {

    //1.生成文件
    /**
     *  DAPM_KFTJ_#590_D_211123_W_NW_F1.ITF
     */

    private static  final String MID_NAME_FILE =  "_#590_D_";
    private static  final String END_NAME_FILE =  "_W_NW_F1.ITF";

    private static String lineSeparator = "\n";
    static {
        try {
            lineSeparator = System.getProperty("line.separator","\n");
        } catch (Exception e) {
            e.printStackTrace();
            lineSeparator = "\n";
        }
    }
    /**
     *
     * @param targetAddr 目标地址
     * @param tableName  表名
     * @return
     */
    public static String createFile(String targetAddr,String tableName){
        String dateStr = "";//yyyy-MM-dd
        String realAddr = "";
        try {
            dateStr = DateUtil.format(new Date(), "yyMMdd");
            String realFile = tableName + MID_NAME_FILE + dateStr + END_NAME_FILE;
            realAddr = targetAddr + File.separator + realFile;
            FileUtil.newFile(realAddr);
        } catch (Exception e) {
            e.printStackTrace();
            return "1";
        }
        return realAddr;
    }

    //2.写入数据
    public static void write(List<Map<String,Object>> list,String targetAddr,String separator){
//        String str = "";
        StringBuffer str = new StringBuffer();
        for (Map<String,Object> map:list) {
            Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();
//            String lineStr = "";
            StringBuffer lineStr = new StringBuffer();
            while (it.hasNext()) {
                Map.Entry<String, Object> itEntry = it.next();
                String itKey = itEntry.getKey();
                Object itValue = itEntry.getValue();
                //System.out.println("key:" + itKey + " value:" + itValue);
                /*if("".equals(lineStr)){
                    lineStr = StrUtil.toString(itValue);
                }else{
                    lineStr = lineStr + separator + itValue;
                }*/
                if (lineStr.length() == 0) {
                    lineStr.append(itValue);
                } else {
                    lineStr.append(separator).append(itValue);
                }
            }
            //FileUtil.appendString(lineStr.toString(),targetAddr,"UTF-8");
            if ("".equals(str)) {
//                str = lineStr + lineSeparator;
                str.append(lineStr).append(lineSeparator);
            } else {
//                str = str + lineStr + lineSeparator;
                str.append(lineStr).append(lineSeparator);
            }

        }
        FileUtil.appendString(str.toString(),targetAddr,"UTF-8");
    }


    public static void writeFile(List<Map<String,Object>> list,String targetAddr,String tableName,String separator){
        String realAddr = createFile(targetAddr,tableName);
        write(list,realAddr,separator);
    }


    public static void main(String[] args) {
        String lineSpe = System.getProperty("line.separator","\n");
        String spe = "\u0005";
        System.out.println(lineSpe);
        String targetAddr = "D:\\work\\java\\springbootw\\springboot-learn\\common-util\\target";

        List<Map<String,Object>> list = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        for (int i = 0; i <100000 ; i++) {
            map = new HashMap<>();
            map.put("a", RandomUtil.randomString(5));
            map.put("b", RandomUtil.randomString(5));
            map.put("c", RandomUtil.randomString(5));
            map.put("d", RandomUtil.randomString(5));
            map.put("e", RandomUtil.randomString(5));
            list.add(map);
        }

        long startTime = System.currentTimeMillis();
        DbToFileUtil.writeFile(list,targetAddr,"DAPM_KFTJ",spe);
        long endTime = System.currentTimeMillis();

        System.out.println("执行时间：" + (endTime-startTime));

    }
}
